<!DOCTYPE html>
<html lang="en" dir="ltr" class="client-nojs">
<head>
<title>Array initialization - cppreference.com</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-css:7:472787eddcf4605d11de8c7ef047234f */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/array_initialization","wgTitle":"c/language/array initialization","wgCurRevisionId":140544,"wgArticleId":13895,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":[],"wgBreakFrames":false,"wgPageContentLanguage":"en","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgMonthNamesShort":["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"wgRelevantPageName":"c/language/array_initialization","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"en","language":"en","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-ColiruCompiler":1,"gadget-MathJax":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_en_:resourceloader:filter:minify-js:7:9f05c6caceb9bb1a482b6cebd4c5a330 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_array_initialization skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">Array initialization</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">From cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language"> Language</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header"> Headers</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types"> Type support</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program"> Program utilities</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic"> Variadic function support</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error"> Error handling</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory"> Dynamic memory management</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string"> Strings library</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm"> Algorithms</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric"> Numerics</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono"> Date and time utilities</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io"> Input/output support</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale"> Localization support</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread"> Concurrency support</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental"> Technical Specifications</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../index.html" title="c/symbol index"> Symbol index</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language"> C language</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="basic_concepts.html" title="c/language/basic concepts">Basic concepts</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../keyword.html" title="c/keyword"> Keywords</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../preprocessor.html" title="c/preprocessor"> Preprocessor</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="statements.html" title="c/language/statements"> Statements</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="expressions.html" title="c/language/expressions"> Expressions</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="initialization.html" title="c/language/initialization"> Initialization</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="c/language/declarations"> Declarations</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="functions.html" title="c/language/functions"> Functions</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> Miscellaneous</td></tr>
<tr class="t-nv"><td colspan="5"> <a href="history.html" title="c/language/history"> History of C</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">Technical Specifications</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="initialization.html" title="c/language/initialization"> Initialization</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="initialization.html#Explicit_initialization" title="c/language/initialization"> Explicit initialization</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="initialization.html#Implicit_initialization" title="c/language/initialization"> Implicit initialization</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="initialization.html#Empty_initialization" title="c/language/initialization"> Empty initialization</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="scalar_initialization.html" title="c/language/scalar initialization"> Scalar initialization</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> Array initialization</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="struct_initialization.html" title="c/language/struct initialization"> Struct and union initialization</a> </td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>When <a href="initialization.html" title="c/language/initialization">initializing</a> an object of <a href="array.html" title="c/language/array">array</a> type, the initializer must be either a <a href="string_literal.html" title="c/language/string literal">string literal</a> (optionally enclosed in braces) or be a brace-enclosed list of initialized for array members:
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>=</b></code> <span class="t-spar">string-literal</span>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>=</b></code> <code><b>{</b></code> <span class="t-spar">expression</span> <code><b>,</b></code> <code><b>...</b></code> <code><b>}</b></code>
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-until-c99">(until C99)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>=</b></code> <code><b>{</b></code> <span class="t-spar">designator</span><span class="t-mark">(optional)</span> <span class="t-spar">expression</span> <code><b>,</b></code> <code><b>...</b></code> <code><b>}</b></code>
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-since-c99">(since C99)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>=</b></code> <code><b>{</b></code> <code><b>}</b></code>
</td>
<td> (3)
</td>
<td> <span class="t-mark-rev t-since-c23">(since C23)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> string literal initializer for character and wide character arrays</div>
<div class="t-li1"><span class="t-li">2)</span> comma-separated list of <span class="t-rev-inl t-until-c99"><span>constant</span> <span><span class="t-mark-rev t-until-c99">(until C99)</span></span></span> expressions that are initializers for array elements<span class="t-rev-inl t-since-c99"><span>, optionally using array designators of the form <code><b>[</b></code> <span class="t-spar">constant-expression</span> <code><b>]</b></code> <code><b>=</b></code> </span> <span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span></div>
<div class="t-li1"><span class="t-li">3)</span> empty initializer empty-initializes every element of the array</div>
<p>Arrays of known size and arrays of unknown size may be initialized<span class="t-rev-inl t-since-c99 t-until-c23"><span>, but not VLAs.</span> <span><span class="t-mark-rev t-since-c99">(since C99)</span><span class="t-mark-rev t-until-c23">(until C23)</span></span></span> <span class="t-rev-inl t-since-c23"><span>A VLA can only be empty-initialized.</span> <span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span>
</p><p>All array elements that are not initialized explicitly are <a href="initialization.html#Empty_initialization" title="c/language/initialization">empty-initialized</a>.
</p>
<h3><span class="mw-headline" id="Initialization_from_strings">Initialization from strings</span></h3>
<p><a href="string_literal.html" title="c/language/string literal">String literal</a> (optionally enclosed in braces) may be used as the initializer for an array of matching type:
</p>
<ul><li> ordinary string literals<span class="t-rev-inl t-since-c11"><span> and UTF-8 string literals</span> <span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span> can initialize arrays of any character type (<span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">char</span></span></span>, <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">signed</span> <span class="kw4">char</span></span></span>, <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">unsigned</span> <span class="kw4">char</span></span></span>)
</li><li> L-prefixed wide string literals can be used to initialize arrays of any type compatible with (ignoring cv-qualifications) <span class="t-lc">wchar_t</span>
</li></ul>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c11"><td>
<ul><li> u-prefixed wide string literals can be used to initialize arrays of any type compatible with (ignoring cv-qualifications) <span class="t-lc">char16_t</span>
</li><li> U-prefixed wide string literals can be used to initialize arrays of any type compatible with (ignoring cv-qualifications) <span class="t-lc">char32_t</span>
</li></ul>
</td>
<td><span class="t-mark-rev t-since-c11">(since C11)</span></td></tr>
</table>
<p>Successive bytes of the string literal or wide characters of the wide string literal, including the terminating null byte/character, initialize the elements of the array:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">char</span> str<span class="br0">[</span><span class="br0">]</span> <span class="sy1">=</span> <span class="st0">"abc"</span><span class="sy4">;</span> <span class="co1">// str has type char[4] and holds 'a', 'b', 'c', '\0'</span>
<span class="kw4">wchar_t</span> wstr<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span> <span class="sy1">=</span> L<span class="st0">"猫"</span><span class="sy4">;</span> <span class="co1">// str has type wchar_t[4] and holds L'猫', '\0', '\0', '\0'</span></pre></div></div>
<p>If the size of the array is known, it may be one less than the size of the string literal, in which case the terminating null character is ignored:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">char</span> str<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <span class="st0">"abc"</span><span class="sy4">;</span> <span class="co1">// str has type char[3] and holds 'a', 'b', 'c'</span></pre></div></div>
<p>Note that the contents of such array are modifiable, unlike when accessing a string literal directly with <span class="t-c"><span class="mw-geshi c source-c"><span class="kw4">char</span><span class="sy2">*</span> str <span class="sy1">=</span> <span class="st0">"abc"</span><span class="sy4">;</span></span></span>.
</p>
<h3><span class="mw-headline" id="Initialization_from_brace-enclosed_lists">Initialization from brace-enclosed lists</span></h3>
<p>When an array is initialized with a brace-enclosed list of initializers, the first initializer in the list initializes the array element at index zero<span class="t-rev-inl t-since-c99"><span> (unless a designator is specified)</span> <span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>, and each subsequent initializer <span class="t-rev-inl t-since-c99"><span>without a designator </span> <span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>initializes the array element at index one greater than the one initialized by the previous initializer.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> x<span class="br0">[</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// x has type int[3] and holds 1,2,3</span>
<span class="kw4">int</span> y<span class="br0">[</span><span class="nu0">5</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// y has type int[5] and holds 1,2,3,0,0</span>
<span class="kw4">int</span> z<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// z has type int[4] and holds 1,0,0,0</span>
<span class="kw4">int</span> w<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">0</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// w has type int[3] and holds all zeroes</span></pre></div></div>
<p>It's an error to provide more initializers than elements when initializing an array of known size (except when initializing character arrays from string literals).
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<p>A designator causes the following initializer to initialize of the array element described by the designator. Initialization then continues forward in order, beginning with the next element after the one described by the designator.
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> n<span class="br0">[</span><span class="nu0">5</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">5</span>,<span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">1</span>,<span class="nu0">2</span>,<span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// holds 1,2,3,4,5</span>
 
<span class="kw4">int</span> a<span class="br0">[</span>MAX<span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span> <span class="co1">// starts initializing a[0] = 1, a[1] = 3, ...</span>
    <span class="nu0">1</span>, <span class="nu0">3</span>, <span class="nu0">5</span>, <span class="nu0">7</span>, <span class="nu0">9</span>, <span class="br0">[</span>MAX<span class="sy2">-</span><span class="nu0">5</span><span class="br0">]</span> <span class="sy1">=</span> <span class="nu0">8</span>, <span class="nu0">6</span>, <span class="nu0">4</span>, <span class="nu0">2</span>, <span class="nu0">0</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="co1">// for MAX=6,  array holds 1,8,6,4,2,0</span>
<span class="co1">// for MAX=13, array holds 1,3,5,7,9,0,0,0,8,6,4,2,0 ("sparse array")</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-c99">(since C99)</span></td></tr>
</table>
<p>When initializing an array of unknown size, the largest subscript for which an initializer is specified determines the size of the array being declared.
</p>
<h3><span class="mw-headline" id="Nested_arrays">Nested arrays</span></h3>
<p>If the elements of an array are arrays, structs, or unions, the corresponding initializers in the brace-enclosed list of initializers are any initializers that are valid for those members, except that their braces may be omitted as follows:
</p><p>If the nested initializer begins with an opening brace, the entire nested initializer up to its closing brace initializes the corresponding array element:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> y<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span> <span class="co1">// array of 4 arrays of 3 ints each (4x3 matrix)</span>
    <span class="br0">{</span> <span class="nu0">1</span> <span class="br0">}</span>,      <span class="co1">// row 0 initialized to {1, 0, 0}</span>
    <span class="br0">{</span> <span class="nu0">0</span>, <span class="nu0">1</span> <span class="br0">}</span>,   <span class="co1">// row 1 initialized to {0, 1, 0}</span>
    <span class="br0">{</span> <span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">1</span> <span class="br0">}</span>,  <span class="co1">// row 2 initialized to {0, 0, 1}</span>
<span class="br0">}</span><span class="sy4">;</span>              <span class="co1">// row 3 initialized to {0, 0, 0}</span></pre></div></div>
<p>If the nested initializer does not begin with an opening brace, only enough initializers from the list are taken to account for the elements or members of the sub-array, struct or union; any remaining initializers are left to initialize the next array element:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> y<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span>    <span class="co1">// array of 4 arrays of 3 ints each (4x3 matrix)</span>
<span class="nu0">1</span>, <span class="nu0">3</span>, <span class="nu0">5</span>, <span class="nu0">2</span>, <span class="nu0">4</span>, <span class="nu0">6</span>, <span class="nu0">3</span>, <span class="nu0">5</span>, <span class="nu0">7</span> <span class="co1">// row 0 initialized to {1, 3, 5}</span>
<span class="br0">}</span><span class="sy4">;</span>                        <span class="co1">// row 1 initialized to {2, 4, 6}</span>
                          <span class="co1">// row 2 initialized to {3, 5, 7}</span>
                          <span class="co1">// row 3 initialized to {0, 0, 0}</span>
 
<span class="kw1">struct</span> <span class="br0">{</span> <span class="kw4">int</span> a<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span>, b<span class="sy4">;</span> <span class="br0">}</span> w<span class="br0">[</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span> <span class="br0">{</span> <span class="nu0">1</span> <span class="br0">}</span>, <span class="nu0">2</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// array of structs</span>
   <span class="co1">// { 1 } is taken to be a fully-braced initializer for element #0 of the array</span>
   <span class="co1">// that element is initialized to { {1, 0, 0}, 0}</span>
   <span class="co1">// 2 is taken to be the first initialized for element #1 of the array</span>
   <span class="co1">// that element is initialized { {2, 0, 0}, 0}</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c99"><td>
<p>Array designators may be nested; the bracketed constant expression for nested arrays follows the bracketed constant expression for the outer array:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> y<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">1</span>, <span class="br0">[</span><span class="nu0">1</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">1</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">1</span>, <span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span>  <span class="co1">// row 0 initialized to {1, 0, 0}</span>
                                               <span class="co1">// row 1 initialized to {0, 1, 0}</span>
                                               <span class="co1">// row 2 initialized to {1, 0, 0}</span>
                                               <span class="co1">// row 3 initialized to {0, 0, 0}</span></pre></div></div>
</td>
<td><span class="t-mark-rev t-since-c99">(since C99)</span></td></tr>
</table>
<h3><span class="mw-headline" id="Notes">Notes</span></h3>
<p>The <a href="eval_order.html" title="c/language/eval order">order of evaluation</a> of subexpressions in an array initializer is indeterminately sequenced in C (but not in C++ since C++11):
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
<span class="kw4">int</span> a<span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span>n<span class="sy2">++</span>, n<span class="sy2">++</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// unspecified, but well-defined behavior,</span>
                       <span class="co1">// n is incremented twice (in arbitrary order)</span>
                       <span class="co1">// a initialized to {1, 2} and to {2, 1} are both valid</span>
<a href="../io/puts.html"><span class="kw836">puts</span></a><span class="br0">(</span><span class="br0">(</span><span class="kw4">char</span><span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">)</span><span class="br0">{</span><span class="st0">'0'</span><span class="sy2">+</span>n<span class="br0">}</span> <span class="sy2">+</span> n<span class="sy2">++</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// undefined behavior:</span>
                              <span class="co1">// increment and read from n are unsequenced</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-until-c23"><td>
<p>In C, the braced list of an initializer cannot be empty. C++ allows empty list:
</p>
</td>
<td><span class="t-mark-rev t-until-c23">(until C23)</span></td></tr>
<tr class="t-rev t-since-c23"><td>
<p>An empty initializer can be used to initialize an array:
</p>
</td>
<td><span class="t-mark-rev t-since-c23">(since C23)</span></td></tr>
</table>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> a<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">0</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// valid C and C++ way to zero-out a block-scope array</span>
<span class="kw4">int</span> a<span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// valid C++ way to zero-out a block-scope array; valid in C since C23</span></pre></div></div>
<p>As with all other <a href="initialization.html" title="c/language/initialization">initialization</a>, every expression in the initializer list must be a <a href="constant_expression.html" title="c/language/constant expression">constant expression</a> when initializing arrays of static or thread-local <a href="storage_duration.html" title="c/language/storage duration">storage duration</a>:
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">static</span> <span class="kw4">char</span><span class="sy2">*</span> p<span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><a href="../memory/malloc.html"><span class="kw403">malloc</span></a><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span>, <a href="../memory/malloc.html"><span class="kw403">malloc</span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// error</span></pre></div></div>
<h3><span class="mw-headline" id="Example">Example</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">// The following four array declarations are the same</span>
    <span class="kw4">short</span> q1<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span>
        <span class="br0">{</span> <span class="nu0">1</span> <span class="br0">}</span>,
        <span class="br0">{</span> <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">}</span>,
        <span class="br0">{</span> <span class="nu0">4</span>, <span class="nu0">5</span>, <span class="nu0">6</span> <span class="br0">}</span>
    <span class="br0">}</span><span class="sy4">;</span>
 
    <span class="kw4">short</span> q2<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="nu0">4</span>, <span class="nu0">5</span>, <span class="nu0">6</span><span class="br0">}</span><span class="sy4">;</span>
 
    <span class="kw4">short</span> q3<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span>
        <span class="br0">{</span>
            <span class="br0">{</span> <span class="nu0">1</span> <span class="br0">}</span>,
        <span class="br0">}</span>,
        <span class="br0">{</span>
            <span class="br0">{</span> <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">}</span>,
        <span class="br0">}</span>,
        <span class="br0">{</span>
            <span class="br0">{</span> <span class="nu0">4</span>, <span class="nu0">5</span> <span class="br0">}</span>,
            <span class="br0">{</span> <span class="nu0">6</span> <span class="br0">}</span>,
        <span class="br0">}</span>
    <span class="br0">}</span><span class="sy4">;</span>
 
    <span class="kw4">short</span> q4<span class="br0">[</span><span class="nu0">4</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">3</span><span class="br0">]</span><span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span><span class="nu0">1</span>, <span class="br0">[</span><span class="nu0">1</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">2</span>, <span class="nu0">3</span>, <span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="sy1">=</span><span class="nu0">4</span>, <span class="nu0">5</span>, <span class="nu0">6</span><span class="br0">}</span><span class="sy4">;</span>
 
 
    <span class="co1">// Character names can be associated with enumeration constants</span>
    <span class="co1">// using arrays with designators:</span>
    <span class="kw2">enum</span> <span class="br0">{</span> RED, GREEN, BLUE <span class="br0">}</span><span class="sy4">;</span>
    <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span>nm<span class="br0">[</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span>
        <span class="br0">[</span>RED<span class="br0">]</span> <span class="sy1">=</span> <span class="st0">"red"</span>,
        <span class="br0">[</span>GREEN<span class="br0">]</span> <span class="sy1">=</span> <span class="st0">"green"</span>,
        <span class="br0">[</span>BLUE<span class="br0">]</span> <span class="sy1">=</span> <span class="st0">"blue"</span>,
    <span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
</div>
<h3><span class="mw-headline" id="References">References</span></h3>
<div class="t-ref-std-17">
<ul><li> C17 standard (ISO/IEC 9899:2018): 
</li></ul>
<dl><dd><ul><li> 6.7.9/12-39 Initialization (p: 101-105)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-11">
<ul><li> C11 standard (ISO/IEC 9899:2011): 
</li></ul>
<dl><dd><ul><li> 6.7.9/12-38 Initialization (p: 140-144)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c99">
<ul><li> C99 standard (ISO/IEC 9899:1999): 
</li></ul>
<dl><dd><ul><li> 6.7.8/12-38 Initialization (p: 126-130)
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c89">
<ul><li> C89/C90 standard (ISO/IEC 9899:1990): 
</li></ul>
<dl><dd><ul><li> 6.5.7 Initialization 
</li></ul>
</dd></dl>
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 1919/1000000
Preprocessor generated node count: 5945/1000000
Post‐expand include size: 40533/2097152 bytes
Template argument size: 12039/2097152 bytes
Highest expansion depth: 13/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:13895-0!*!0!!en!*!* and timestamp 20220624090641 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    Retrieved from "<a href="https://en.cppreference.com/mwiki/index.php?title=c/language/array_initialization&amp;oldid=140544">https://en.cppreference.com/mwiki/index.php?title=c/language/array_initialization&amp;oldid=140544</a>"                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>Navigation</h5>
            <ul><li><a href="https://en.cppreference.com/w/c/language/array_initialization">Online version</a></li><li>Offline version retrieved 2022-07-30 14:05.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> This page was last modified on 24 June 2022, at 02:06.</li>
                                    <li id="footer-info-viewcount">This page has been accessed 984,686 times.</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.ColiruCompiler","ext.gadget.MathJax"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.035 secs. -->
	</body>
<!-- Cached 20220624090724 -->
</html>